<?php

class LoginAction extends Action
{
    
    /**
     * @index 调出登陆模板
     * 
     * 
     * 
     **/

    public function index()
    {
        $this->display();
    }
    
    /**
     * @login 登陆处理
     * 
     * 
     * 
     **/

    public function login() {
        import('ORG.Net.Api');
        $apiurl = new Api();
        $user = $_POST['username'];
        $pwd = md5($_POST['password']);  
        $api=$apiurl->login($user,$pwd);        
        $url = file_get_contents($api);
        $info = json_decode($url, true);
        $img=$apiurl->img($info['img_id'],140,140);
        if ($info['ret'] == true) {
            $_SESSION['username'] = $info['user_name'];
            $_SESSION['ibg_k'] = $info['ibg_kind'];
            $_SESSION['phone'] = $info['phone'];
            $_SESSION['s_id'] = $info['session_id'];
            $_SESSION['phone_uid'] = $info['phone_uid'];
            $_SESSION['safe_md5'] = $info['safe_md5'];
            $_SESSION['img'] = $img;
            $this->success("登陆成功!", U('user/index'));
        } else {
            $this->error("密码或用户名/手机错误!", U('login/index'));
        }

    }
    
    /**
     * @reg 调出注册模板
     * 
     * 
     * 
     **/

    public function reg()
    {
        $this->display();
    }
    
    /**
     * @toreg 注册处理
     * 
     * 
     * 
     **/

    public function toreg()    {
        import('ORG.Net.Api');
        $apiurl = new Api();
        //传入注册参数
        $name = $_POST['username'];
        $phone = $_POST['mob'];
        $pass = md5($_POST['password']);
        $api=$apiurl->reg($phone,$pass,$name,IBUGER);
        //调用API接口
        $reg = file_get_contents($api);
        $info = json_decode($reg, true);
        //处理返回信息
        if ($info['ret'] == true) {
            $this->success("注册成功!请登陆", U('login/index'));
        } else {
            $this->error($info['msg'], U('login/reg'));
        }

    }
    
    /**
     * @logout 退出处理(删除session)
     * 
     * 
     * 
     **/

    public function logout()
    {
        $_SESSION = array();
        if (isset($_COOKIE[session_name()])) {
            setcookie(session_name(), '', time() - 3600, '/');
        }
        session_destroy();
        $this->redirect('index/index');
    }


}
